//
//  KATToast.m
//  KATFramework
//
//  Created by Kantice on 16/2/20.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATToast.h"



@interface KATToast ()

///是否在显示
@property(nonatomic,readonly,assign) BOOL isShown;

///定时器
@property(nonatomic,retain) NSTimer *timer;

///标签控件
@property(nonatomic,retain) KATLabel *label;

///按钮
@property(nonatomic,retain) KATButton *button;


@end



@implementation KATToast


//获取实例
+ (instancetype)toast
{
    return [self toastWithButton:KATButtonTypeNone text:nil style:nil duration:TOAST_DURATION_NORMAL andPosition:TOAST_POSITION_BOTTOM];
}


//获取实例
+ (instancetype)toastWithButton:(KATButtonType)type
{
    return [self toastWithButton:type text:nil style:nil duration:TOAST_DURATION_NORMAL andPosition:TOAST_POSITION_BOTTOM];
}


//获取实例
+ (instancetype)toastWithButton:(KATButtonType)type andText:(NSString *)text
{
    return [self toastWithButton:type text:text style:nil duration:TOAST_DURATION_NORMAL andPosition:TOAST_POSITION_BOTTOM];
}


//获取实例
+ (instancetype)toastWithText:(NSString *)text
{
    return [self toastWithButton:KATButtonTypeNone text:text style:nil duration:TOAST_DURATION_NORMAL andPosition:TOAST_POSITION_BOTTOM];
}


//获取实例
+ (instancetype)toastWithText:(NSString *)text andDuration:(float)duration
{
    return [self toastWithButton:KATButtonTypeNone text:text style:nil duration:duration andPosition:TOAST_POSITION_BOTTOM];
}


//获取实例
+ (instancetype)toastWithText:(NSString *)text andStyle:(KATTextStyle *)style
{
    return [self toastWithButton:KATButtonTypeNone text:text style:style duration:TOAST_DURATION_NORMAL andPosition:TOAST_POSITION_BOTTOM];
}


//获取实例
+ (instancetype)toastWithButton:(KATButtonType)type text:(NSString *)text style:(KATTextStyle *)style duration:(float)duration andPosition:(int)position
{

    KATToast *toast=[[[self alloc] init] autorelease];
        
    //样式
    toast.contentWidth=MIN(SCREEN_WIDTH,SCREEN_HEIGHT)*0.62;
    toast.contentPadding=12.0;
    
    if(!style)
    {
        style=[KATTextStyle textStyle];
        style.textFont=FONT_CALLOUT_S;
        style.marginLeft=0.0;
        style.marginRight=0.0;
        style.marginTop=0.0;
        style.marginBottom=0.0;
        style.hasPhoneStyle=NO;
        style.textColor=KCOLOR_WHITE;
    }
    
    style.selectable=NO;
    style.editable=NO;
    
    toast.hidden=YES;
    toast.backgroundColor=KCOLOR_SHADOW_DARK;
    toast.clipsToBounds=YES;
    toast.layer.cornerRadius=5.0f;
    
    toast.position=position;
    toast.duration=duration;
    toast.canTap=NO;
    toast.timer=nil;
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:toast action:@selector(hide)];
    [toast addGestureRecognizer:tap];
    [tap release];
    
    toast.label=[KATLabel labelWithFrame:CGRectZero andStyle:nil];
    
    [toast addSubview:toast.label];
    
    toast.button=[KATButton buttonWithFrame:CGRectZero];
    toast.buttonColor=KCOLOR_WHITE;
    
    toast.style=style;
    toast.text=text;
    toast.buttonType=type;
    
    
    //注册屏幕旋转事件通知
    [[NSNotificationCenter defaultCenter] addObserver:toast selector:@selector(screenRotary) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    
    return toast;

}


- (void)setDuration:(float)duration
{
    if(duration>0.0)
    {
        _duration=duration;
    }
}


- (void)setPosition:(int)position
{
    if(position>=TOAST_POSITION_BOTTOM && position<=TOAST_POSITION_TOP)
    {
        _position=position;
        
        [self update];
    }
}


- (void)setCanTap:(BOOL)canTap
{
    self.userInteractionEnabled=canTap;
}


- (void)setText:(NSString *)text
{
    if(_text!=text)
    {
        [_text release];
        _text=[text retain];
        
        [self update];
    }
}


- (void)setStyle:(KATTextStyle *)style
{
    if(style!=_style)
    {
        [_style release];
        _style=[style retain];
        
        [self update];
    }
}


- (void)setContentWidth:(float)contentWidth
{
    _contentWidth=contentWidth;
    
    [self update];
}


- (void)setContentPadding:(float)contentPadding
{
    _contentPadding=contentPadding;
    
    [self update];
}


- (void)setButtonType:(KATButtonType)buttonType
{
    _buttonType=buttonType;
    
    [self update];
}


//更新
- (void)update
{
    if(_buttonType==KATButtonTypeNone)//没有按钮，只有文本
    {
        [_button removeFromSuperview];
        
        _label.frame=CGRectMake(_contentPadding, _contentPadding*0.5, _contentWidth-_contentPadding*2, MIN(SCREEN_WIDTH,SCREEN_HEIGHT)*0.38);
        _label.style=_style;
        _label.text=_text;
        _label.style.textAlignment=NSTextAlignmentCenter;
        [_label resizeFrame];
        
        self.bounds=RECT(0, 0, _label.frame.size.width+2*_contentPadding, _label.frame.size.height+_contentPadding);
    }
    else//有按钮
    {
        if(_text && _text.length>0)//有文本
        {
            //文本
            _label.frame=RECT(_contentPadding, _contentPadding, _contentWidth-_contentPadding*2-_style.textFont.lineHeight, MIN(SCREEN_WIDTH,SCREEN_HEIGHT)*0.38);
            _label.style=_style;
            _label.text=_text;
            [_label resizeFrame];
                        
            if(_style.textFont.lineHeight+1.0<_label.bounds.size.height)//超过一行
            {
                _label.frame=RECT(_contentPadding, _contentPadding+_style.textFont.lineHeight*2, _contentWidth-_contentPadding*2, MIN(SCREEN_WIDTH,SCREEN_HEIGHT)*0.38);
                [_label resizeFrame];
                
                //按钮
                _button.type=_buttonType;
                _button.lineWidth=1.5;
                _button.scale=0.8;
                _button.color=(_buttonColor?_buttonColor:KCOLOR_WHITE);
                _button.frame=CGRectMake(_contentPadding+(_label.frame.size.width-_style.textFont.lineHeight*2)*0.5, _contentPadding, _style.textFont.lineHeight*2, _style.textFont.lineHeight*2);

                self.bounds=RECT(0, 0, _label.frame.size.width+2*_contentPadding, _button.bounds.size.height+_label.frame.size.height+2*_contentPadding);
            }
            else//一行
            {
                _label.frame=RECT(_contentPadding+_style.textFont.lineHeight,_contentPadding,_label.bounds.size.width,_label.bounds.size.height);
                
                //按钮
                _button.type=_buttonType;
                _button.lineWidth=1.0;
                _button.scale=0.9;
                _button.color=(_buttonColor?_buttonColor:KCOLOR_WHITE);
                _button.frame=CGRectMake(_contentPadding, _contentPadding, _style.textFont.lineHeight, _style.textFont.lineHeight);
                
                self.bounds=RECT(0, 0, _label.frame.size.width+2*_contentPadding+_style.textFont.lineHeight, _label.frame.size.height+2*_contentPadding);
            }
            
            [self addSubview:_button];
        }
        else//只有按钮
        {
            _button.type=_buttonType;
            _button.lineWidth=2.0;
            _button.scale=1.0;
            _button.color=(_buttonColor?_buttonColor:KCOLOR_WHITE);
            _button.frame=CGRectMake(_contentPadding, _contentPadding, _style.textFont.lineHeight*2, _style.textFont.lineHeight*2);
            
            self.bounds=RECT(0, 0, _button.bounds.size.width+2*_contentPadding, _button.bounds.size.height+2*_contentPadding);
            
            [self addSubview:_button];
        }
    }
    
    if(_position==TOAST_POSITION_TOP)
    {
        self.center=POINT(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.25);
    }
    else if(_position==TOAST_POSITION_CENTER)
    {
        self.center=POINT(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.5);
    }
    else
    {
        self.center=POINT(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.75);
    }

    
    //如果在显示中，则重新计时
    if(_isShown)
    {
        if(_timer)
        {
            [_timer invalidate];
            
            self.timer=nil;
        }
        
        //重开定时器
        self.timer=[NSTimer scheduledTimerWithTimeInterval:_duration target:self selector:@selector(hide) userInfo:nil repeats:NO];
    }
}


//显示
- (void)show
{
    if(!_isShown && !_showing)
    {
        //关闭定时器
        if(_timer)
        {
            [_timer invalidate];
            
            self.timer=nil;
        }
        
        //重开定时器
        self.timer=[NSTimer scheduledTimerWithTimeInterval:_duration target:self selector:@selector(hide) userInfo:nil repeats:NO];
        
        [[KATAppUtil keyWindow] addSubview:self];
        
        _showing=YES;
        _isShown=YES;
        
        self.hidden=NO;
        self.alpha=0.05f;
        
        [UIView animateWithDuration:0.3f animations:^
        {
            self.alpha=1.0f;
        }
        completion:^(BOOL finished)
        {
            _showing=NO;
        }];
    }
}


//隐藏
- (void)hide
{
    if(_isShown && !_hiding)
    {
        //关闭定时器
        if(_timer)
        {
            [_timer invalidate];
            
            self.timer=nil;
        }
        
        _hiding=YES;
        _isShown=NO;
        
        [UIView animateWithDuration:0.3f animations:^
        {
            self.alpha=0.0f;
        }
        completion:^(BOOL finished)
        {
            _hiding=NO;
            self.hidden=YES;
            
            [self removeFromSuperview];
        }];
    }
}


//屏幕旋转
- (void)screenRotary
{
    [self update];
}


- (void)dealloc
{
    //取消通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    if(_timer)
    {
        [_timer invalidate];
    }
    
    [_button release];
    [_buttonColor release];
    [_label release];
    [_text release];
    [_style release];
    [_timer release];
    
    [super dealloc];
}


@end
